﻿using Emgu.CV.CvEnum;
using OpenCvApplication.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenCvApplication.View.CommonView
{
    public partial class GaussianBlurParameterControl : UserControl
    {
        public GaussianBlurParameterControl()
        {
            InitializeComponent();

            BindBorderType();

            foreach (Control item in this.Controls)
            {
                if (item is TrackBar trackBar)
                    trackBar.Scroll += trackBar_Scroll;

                if (item is NumericUpDown numericUpDown)
                    numericUpDown.ValueChanged += numericUpDown_ValueChanged;
            }
        }
        private void BindBorderType()
        {
            var enumValues = Enum.GetValues(typeof(BorderType));
            var list = new List<IdValues>();
            int index = 0, curIndex = 0;
            foreach (Enum value in enumValues)
            {
                int hc = value.GetHashCode();
                list.Add(new IdValues
                {
                    Id = hc.ToString(),
                    Value = value.ToString(),
                    Standby = hc
                });
                if (value.ToString() == "Default")
                    index = curIndex;

                curIndex++;
            }

            this.cmbBorderType.ValueMember = "Id";
            this.cmbBorderType.DisplayMember = "Value";
            this.cmbBorderType.DataSource = list;
            this.cmbBorderType.SelectedIndex = index;
        }
        private void trackBar_Scroll(object sender, EventArgs e)
        {
            TrackBar trackBar = (TrackBar)sender;
            if ((trackBar.Value & 1) == 0)
            {
                trackBar.Value++;
            }
            RefreshShow();
            ValueChange?.Invoke(this, Args);
        }

        private void cmbBorderType_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshShow();
            ValueChange?.Invoke(this, Args);
        }

        private void numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            RefreshShow();
            ValueChange?.Invoke(this, Args);
        }

        private void RefreshShow()
        {
            lblSizeWidth.Text = SizeWidth.ToString();
            lblSizeHeight.Text = SizeHeight.ToString();
            lblSigmaX.Text = SigmaX.ToString("0.00");
            lblsigmaY.Text = SigmaY.ToString("0.00");
        }

        private void GaussianBlurParameterControl_Load(object sender, EventArgs e)
        {
            ValueChange?.Invoke(this, Args);
        }

        public Action<object, GaussianBlurEventArgs> ValueChange { get; set; }
        public int SizeWidth
        {
            get => tcbSizeWidth.Value;
        }
        public int SizeHeight
        {
            get => tcbSizeHeight.Value;
        }
        public double SigmaX
        {
            get => tcbSigmaX.Value + (float)numericUpDown1.Value;
        }
        public double SigmaY
        {
            get => tcbsigmaY.Value + (float)numericUpDown2.Value;
        }
        public BorderType BorderTypes
        {
            get => (BorderType)(cmbBorderType.Items[cmbBorderType.SelectedIndex] as IdValues).Standby;
        }
        public GaussianBlurEventArgs Args
        {
            get => new GaussianBlurEventArgs(SizeWidth, SizeHeight, SigmaX, SigmaY, BorderTypes);
        }
    }
    public class GaussianBlurEventArgs : EventArgs
    {
        public GaussianBlurEventArgs() { }
        public GaussianBlurEventArgs(int sizeWidth, int sizeHeight, double sigmaX, double sigmaY, BorderType borderType) : this()
        {
            this.SizeWidth = sizeWidth;
            this.SizeHeight = sizeHeight;
            this.SigmaX = sigmaX;
            this.SigmaY = sigmaY;
            this.BorderTypes = borderType;
        }
        public int SizeWidth { get; set; }
        public int SizeHeight { get; set; }
        public double SigmaX { get; set; }
        public double SigmaY { get; set; }
        public BorderType BorderTypes { get; set; }
    }
}
